package com.cangxuan.backend.controller;

import com.cangxuan.backend.service.ISMSService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.regex.Pattern;

/**
 * 短信接口
 */
@Slf4j
@Api(tags = "短信相关接口")
@RestController
@RequestMapping("sms")
public class SMSController extends BaseController {

    ISMSService smsService;
    @Autowired
    public void setSmsService(ISMSService smsService) {
        this.smsService = smsService;
    }

    /**
     * 发送短信
     *
     * @param mobile
     * @return
     */
    @PostMapping("send")
    @ApiOperation("发送短信")
    public Object send(@ApiParam("手机号码，11位，以13、15、18、19开头") @RequestParam String mobile) {
        Pattern pattern = Pattern.compile("[1][345789][0-9]{9}");
        if (!pattern.matcher(mobile).matches()) {
            return fail(402, "手机号码格式错误");
        }
        smsService.send(mobile);
        return success("发送成功");

    }

}
